package org.example.service;

import org.example.entity.Product;
import org.example.entity.Shanpin;
import org.example.mapper.Shanpinmapper;
import org.example.mapper.Shouchanmapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ShouchanService {
    @Autowired
    Shouchanmapper shouchanmapper;
    @Autowired
    Shanpinmapper shanpinmapper;
    /**
     * 收藏查询
     * @return
     */
    public Object cx(int id,String username) {
        return shouchanmapper.cx(id,username);

    }

    /**
     * 修改收藏
     *
     * @param id
     * @param isFavorite
     * @param username
     */
    public void xg(int id, Boolean isFavorite, String username) {
        if(isFavorite){
            shouchanmapper.sc(id,username);
        }else{
            shouchanmapper.xz(id,username);
        }
    }

    /**
     * 商品查询
     * @return
     */
    public List<Shanpin> shanpincx(String username) {
        List<Product> productList=shanpinmapper.productList();
        List<Shanpin> shanpinList=new ArrayList<>();
        for(Product p:productList){
            if(shouchanmapper.cx(p.getShanpinId(),username)!=null) {
                Shanpin shanpin = shanpinmapper.cxid(p.getShanpinId());
                shanpin.setYingfe(p.getTemplate());
                shanpinList.add(shanpin);
            }
        }
        return  shanpinList;
    }

    /**
     *移入收藏
     * @param id
     * @param username
     */
    public void xz(int id, String username) {
        if(shouchanmapper.cx(id,username)==null){
            shouchanmapper.xz(id,username);
        }
    }

    /**
     * 收藏删除
     *
     * @param id
     * @param username
     */
    public void sc(int id, String username) {
        shouchanmapper.sc(id,username);
    }
}
